package auction.auction;

import auction.dao.AuctionUserDao;
import auction.entity.Auctionuser;
import auction.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserAction {

    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        return "/login.jsp";
    }
    //用户登录方法
    public String dologin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            Auctionuser auctionuser =new Auctionuser();
            auctionuser.setUsername(name);
            auctionuser.setUserpassword(password);
            int count = sqlSession.getMapper(AuctionUserDao.class).UserLogin(auctionuser.getUsername(),auctionuser.getUserpassword());
            if(count>0){
                //储存用户信息
                HttpSession session = request.getSession();
                session.setAttribute("successMsg", name+"登录成功");
                session.setAttribute("username",name);
                return "/do/auction/home";  //  转发到加载home页面的方法
            }else {
                request.setAttribute("msg","账号密码不存在,请检查");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return "/login.jsp";  //  转发
    }

    public String loginAdmin(HttpServletRequest request,HttpServletResponse response){
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            Auctionuser auctionuser =new Auctionuser();
            auctionuser.setUsername(name);
            auctionuser.setUserpassword(password);
            int count = sqlSession.getMapper(AuctionUserDao.class).AdminLogin(auctionuser.getUsername(),auctionuser.getUserpassword());
            System.out.println(count);
            if(count>0){
                return "/do/auction/homeAdmin";
            }else {
                request.setAttribute("msg","账号密码不存在，请检查");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return  "/loginAdmin.jsp";
    }
    public String register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "redirect:/register.jsp";  //redirect 重定向
    }
    public String doregister(HttpServletRequest request, HttpServletResponse response){

        SqlSession sqlSession=null;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            Auctionuser auctionuser =new Auctionuser();
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            String idCard=request.getParameter("idCard");
            String tel=request.getParameter("tel");
            String address=request.getParameter("address");
            String postNumber=request.getParameter("postNumber");
            auctionuser.setUsername(username);
            auctionuser.setUserpassword(password);
            auctionuser.setUsercardno(idCard);
            auctionuser.setUsertel(tel);
            auctionuser.setUseraddress(address);
            auctionuser.setUserpostnumber(postNumber);
            int count = sqlSession.getMapper(AuctionUserDao.class).RegisterUser(auctionuser);
            if(count>0){
                return "/login.jsp";
            }else {
                request.setAttribute("msg","注册失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return "redirect:/register.jsp";
    }

    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
